package com.framework.service.app.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.BannerDao;
import com.framework.entity.or.OrBanner;
import com.framework.entity.or.OrBannerType;
import com.framework.page.PageUtils;
import com.framework.page.Query;
import com.framework.service.app.BannerTypeService;
import com.framework.service.app.DiscoverImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service("discoverImgService")
public class DiscoverImgServiceImpl extends ServiceImpl<BannerDao,OrBanner> implements DiscoverImgService {


    @Autowired
    private BannerTypeService bannerTypeService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Integer state = 0;

        if (params.get("state") != null) {
            state = Integer.valueOf(params.get("state").toString());
        }
        List<OrBannerType> bannerTypes =bannerTypeService.queryListByParentType(3);

        List<Integer> typeList = bannerTypes.stream().map(OrBannerType::getType).collect(Collectors.toList());

        Integer type=0;
        if (params.get("type") != null) {
            type = Integer.valueOf(params.get("type").toString());
        }

        Page<OrBanner> page = selectPage(new Query<OrBanner>(params).getPage(), new EntityWrapper<OrBanner>()
                .eq("dept_id", params.get("deptId"))
                .eq(state != 0, "state", state)
                .in(type==0,"type", typeList)
                .eq(type!=0,"type",type)
        );

        return new PageUtils(page);
    }

    @Override
    public Integer querySort(Integer type, Long deptId) {
        List<OrBanner> orBanners = selectList(new EntityWrapper<OrBanner>().eq("type", type).eq("dept_id", deptId).orderBy("sort", false));
        Integer sort;
        if(orBanners!=null && orBanners.size()>0){
            sort = orBanners.get(0).getSort();
        }else {
            sort=0;
        }
        return ++sort;
    }
}

